package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallGoodsMapper;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallGoods.Column;
import org.linlinjava.litemall.db.domain.LitemallGoodsExample;
import org.linlinjava.litemall.db.domain.LitemallGoodsVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Service
public class LitemallGoodsService {
    @Resource
    private LitemallGoodsMapper goodsMapper;

    public List<LitemallGoods> findByIds(List<Integer> catList){
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andIdIn(catList).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        return goodsMapper.selectByExample(example);
    }
    public List<LitemallGoods> queryByHot(int offset, int limit,int categoryId) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andIsHotEqualTo(true).andDeletedEqualTo(false).andIsOnSaleEqualTo(true).andCategoryIdNotEqualTo(categoryId);
        example.orderBy(Column.addTime.desc());
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectByExample(example);
    }

    public List<LitemallGoods> queryCategory(int offset,int limit,int categoryId){
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andDeletedEqualTo(false).andIsOnSaleEqualTo(true).andCategoryIdEqualTo(categoryId);
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectByExample(example);
    }

    public List<LitemallGoods> queryAll(int offset, int limit){
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectByExample(example);
    }

    public List<LitemallGoods> queryByNew(int offset, int limit,int cid) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andIsNewEqualTo(true).andDeletedEqualTo(false).andIsOnSaleEqualTo(true).andCategoryIdNotEqualTo(cid);
        example.orderBy(Column.addTime.desc());
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectByExample(example);
    }

    public List<LitemallGoods> queryByCategory(List<Integer> catList, int offset, int limit) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andCategoryIdIn(catList).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);

            example.orderBy(LitemallGoods.Column.addTime.desc());
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectByExample(example);
    }

    public List<LitemallGoodsVo> queryByCategorys(List<Integer> catList, Integer integral, Integer price, Integer count, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return goodsMapper.queryByCategoryss(catList, integral, price, count);
    }

    public int countByCategory(List<Integer> catList, int offset, int limit) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andCategoryIdIn(catList).andDeletedEqualTo(false);
        return (int)goodsMapper.countByExample(example);
    }

    public List<LitemallGoods> queryByCategory(Integer catId, int offset, int limit) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andCategoryIdEqualTo(catId).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectByExample(example);
    }

    public int countByCategory(Integer catId, Integer page, Integer size) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andCategoryIdEqualTo(catId).andDeletedEqualTo(false);
        return (int)goodsMapper.countByExample(example);
    }

    public List<LitemallGoods> querySelective(Integer catId, Integer brandId, String keyword, Integer isHot, Integer isNew, Integer offset, Integer limit, String sort,List<Integer> cateList) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdNotIn(cateList);
        if(catId != null && catId != 0){
            criteria.andCategoryIdEqualTo(catId);
        }
        if(brandId != null){
            criteria.andNewBrandIdEqualTo(brandId);
        }
        if(isNew != null){
            criteria.andIsNewEqualTo(isNew.intValue() == 1);
        }
        if(isHot != null){
            criteria.andIsHotEqualTo(isHot.intValue() == 1);
        }
        if(keyword != null){
            criteria.andKeywordsLike("%" + keyword + "%");

            LitemallGoodsExample.Criteria criteria2 = example.createCriteria();
            criteria2.andNameLike("%" + keyword + "%");
            criteria2.andDeletedEqualTo(false);
            criteria2.andIsOnSaleEqualTo(true);
            criteria2.andCategoryIdNotIn(cateList);
            example.or(criteria2);
        }
        criteria.andDeletedEqualTo(false);
        criteria.andIsOnSaleEqualTo(true);
        if(sort != null){
            example.setOrderByClause(sort);
        }
        example.orderBy(LitemallGoods.Column.addTime.desc());
        if(limit != null && offset != null) {
            PageHelper.startPage(offset, limit);
        }

        Column[] columns = new Column[]{Column.id, Column.name, Column.picUrl,Column.counterPrice, Column.retailPrice,Column.categoryId, Column.integral, Column.priceFlag, Column.integralFlag};
        return goodsMapper.selectByExampleSelective(example ,columns);
    }

    public List<LitemallGoods> querySelective(Integer catId, Integer brandId, String keyword, Integer isHot, Integer isNew, Integer offset, Integer limit, String sort,Integer categoryPiLiang) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryPiLiang);
        if(catId != null && catId != 0){
            criteria.andCategoryIdEqualTo(catId);
        }
        if(brandId != null){
            criteria.andBrandIdEqualTo(brandId);
        }
        if(isNew != null){
            criteria.andIsNewEqualTo(isNew.intValue() == 1);
        }
        if(isHot != null){
            criteria.andIsHotEqualTo(isHot.intValue() == 1);
        }
        if(keyword != null){
            criteria.andKeywordsLike("%" + keyword + "%");

            LitemallGoodsExample.Criteria criteria2 = example.createCriteria();
            criteria2.andNameLike("%" + keyword + "%");
            criteria2.andDeletedEqualTo(false);
            criteria2.andIsOnSaleEqualTo(true);
            criteria2.andCategoryIdEqualTo(categoryPiLiang);
            example.or(criteria2);
        }
        criteria.andDeletedEqualTo(false);
        criteria.andIsOnSaleEqualTo(true);
        if(sort != null){
            example.setOrderByClause(sort);
        }
        example.orderBy(LitemallGoods.Column.addTime.desc());
        if(limit != null && offset != null) {
            PageHelper.startPage(offset, limit);
        }

        Column[] columns = new Column[]{Column.id, Column.name, Column.picUrl,Column.counterPrice, Column.retailPrice,Column.categoryId, Column.integral, Column.priceFlag, Column.integralFlag};
        return goodsMapper.selectByExampleSelective(example ,columns);
    }

    public List<LitemallGoodsVo> querySelectives(Integer catId,Integer categoryL2Id, Integer integral, Integer price, Integer count, Integer offset, Integer limit) {
        PageHelper.startPage(offset, limit);
        return goodsMapper.queryByCategorys(categoryL2Id, catId, integral, price, count);
    }

    public List<LitemallGoodsVo> queryByHot(List<Integer> categoryids, Integer offset, Integer limit) {
        PageHelper.startPage(offset, limit);
        return goodsMapper.queryByHot(categoryids);
    }

    public int countSelective(Integer catId, Integer brandId, String keyword, Integer isHot, Integer isNew, Integer offset, Integer limit, String sort) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();

        if(catId != null){
            criteria.andCategoryIdEqualTo(catId);
        }
        if(brandId != null){
            criteria.andBrandIdEqualTo(brandId);
        }
        if(isNew != null){
            criteria.andIsNewEqualTo(isNew.intValue() == 1);
        }
        if(isHot != null){
            criteria.andIsHotEqualTo(isHot.intValue() == 1);
        }
        if(keyword != null){
            criteria.andKeywordsLike("%" + keyword + "%");
        }
        criteria.andDeletedEqualTo(false);
        criteria.andIsOnSaleEqualTo(true);
        return (int)goodsMapper.countByExample(example);
    }

    public LitemallGoods findById(Integer id) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        return goodsMapper.selectOneByExampleWithBLOBs(example);
    }

    public LitemallGoods findByKeyId(Integer id){
        return goodsMapper.selectByPrimaryKey(id);
    }

    public LitemallGoods findGroupGood(Integer id,Integer categoryId) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andIdEqualTo(id).andCategoryIdEqualTo(categoryId).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        return goodsMapper.selectOneByExampleWithBLOBs(example);
    }

    public LitemallGoods findEditById(Integer id) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return goodsMapper.selectOneByExampleWithBLOBs(example);
    }

    public List<LitemallGoods> queryByIds(List<Integer> relatedGoodsIds) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andIdIn(relatedGoodsIds).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        return goodsMapper.selectByExampleWithBLOBs(example);
    }

    public Integer queryOnSale() {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        return (int)goodsMapper.countByExample(example);
    }

    public List<LitemallGoods> querySelective(String goodsSn, String name, Integer categoryId, Integer page, Integer size, String sort, String order,List<Integer> newGoodsIds) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(goodsSn)){
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        if(!StringUtils.isEmpty(newGoodsIds)){
            criteria.andIdIn(newGoodsIds);
        }
        if(!StringUtils.isEmpty(categoryId)){
            criteria.andCategoryIdEqualTo(categoryId);
        }
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, size);
        return goodsMapper.selectByExampleWithBLOBs(example);
    }

    public List<LitemallGoods> queryTotalSelective(String goodsSn, String name, String sort, String order) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(goodsSn)){
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        criteria.andIsOnSaleEqualTo(true);
        return goodsMapper.selectByExampleWithBLOBs(example);
    }

    public List<LitemallGoods> queryTotalSelectives(String goodsSn, String name, String sort, String order) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(goodsSn)){
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        Column[] columns = new Column[]{Column.id,  Column.goodsSn, Column.name,Column.counterPrice,Column.priceFlag, Column.retailPrice, Column.integralFlag, Column.integral, Column.priceFlag,Column.isNew,Column.isHot,Column.isOnSale,Column.brief,Column.unit,Column.keywords};
        return goodsMapper.selectByExampleSelective(example,columns);
    }

    public int countSelective(String goodsSn, String name, Integer categoryId, Integer page, Integer size, String sort, String order,List<Integer> newGoodsIds) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(goodsSn)){
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        if(!StringUtils.isEmpty(newGoodsIds)){
            criteria.andIdIn(newGoodsIds);
        }

        if(!StringUtils.isEmpty(categoryId)){
            criteria.andCategoryIdEqualTo(categoryId);
        }
        criteria.andDeletedEqualTo(false);
        return (int)goodsMapper.countByExample(example);
    }

    public void updateById(LitemallGoods goods) {
        goodsMapper.updateByPrimaryKeySelective(goods);
    }

    public void deleteById(Integer id) {
        goodsMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallGoods goods) {
        goodsMapper.insertSelective(goods);
    }

    public int categoryCount(int categoryId){
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andDeletedEqualTo(false).andIsOnSaleEqualTo(true).andCategoryIdEqualTo(categoryId);
        return (int)goodsMapper.countByExample(example);
    }

    public int count() {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        return (int)goodsMapper.countByExample(example);
    }

    public List<Integer> getCatIds(Integer brandId, String keyword, Integer isHot, Integer isNew) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();

        if(brandId != null){
            criteria.andNewBrandIdEqualTo(brandId);
        }
        if(isNew != null){
            criteria.andIsNewEqualTo(isNew.intValue() == 1);
        }
        if(isHot != null){
            criteria.andIsHotEqualTo(isHot.intValue() == 1);
        }
        if(keyword != null){
            criteria.andKeywordsLike("%" + keyword + "%");
        }
        criteria.andDeletedEqualTo(false);
        criteria.andIsOnSaleEqualTo(true);
        List<LitemallGoods> goodsList = goodsMapper.selectByExampleSelective(example, Column.categoryId);
        List<Integer> cats = new ArrayList<Integer>();
        for(LitemallGoods goods : goodsList){
            cats.add(goods.getCategoryId());
        }
        return cats;
    }

    public boolean checkExistByName(String name) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        example.or().andNameEqualTo(name).andDeletedEqualTo(false);
        return goodsMapper.countByExample(example) != 0;
    }

    public List<LitemallGoods> queryByTopTen(int categoryId) {
        return goodsMapper.queryByTopTen(categoryId,LocalDateTime.now().minus(100, ChronoUnit.DAYS),LocalDateTime.now());
    }

    public Boolean selectById(Integer id) {
        return goodsMapper.selectById(id);
    }

    public List<LitemallGoods> querySelective(String name,Integer categoryId, Integer page, Integer size) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andCategoryIdEqualTo(categoryId);
        criteria.andDeletedEqualTo(false);
        criteria.andIsOnSaleEqualTo(true);
        PageHelper.startPage(page, size);
        return goodsMapper.selectByExampleWithBLOBs(example);
    }

    public int countSeletive(String name ,Integer categoryId) {
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        criteria.andIsOnSaleEqualTo(true);
        criteria.andCategoryIdEqualTo(categoryId);
        return (int) goodsMapper.countByExample(example);
    }
}
